package timer;

import java.util.concurrent.*;

public class ScheduleExecutorStopTest implements Runnable{
    public volatile boolean signClose = false;
    public Future future;



    public static void main(String[] args) throws InterruptedException {
        ScheduleExecutorStopTest stopTest1 = new ScheduleExecutorStopTest();
        ScheduleExecutorStopTest stopTest2 = new ScheduleExecutorStopTest();
        ExecutorService executorService = Executors.newScheduledThreadPool(2);
        stopTest1.future = ((ScheduledExecutorService) executorService).scheduleAtFixedRate(stopTest1,0,1000,TimeUnit.MILLISECONDS);
        Thread.sleep(2000);

        stopTest2.future = ((ScheduledExecutorService) executorService).scheduleAtFixedRate(stopTest2,0,1000,TimeUnit.MILLISECONDS);
        Thread.sleep(5000);
        stopTest1.signClose =true;
        stopTest2.signClose =true;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        if (signClose){
            System.out.println("不执行啦啦1");
            future.cancel(true);
            try {
                Thread.sleep(1000);
                System.out.println("不执行啦啦2");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
